package auth

import (
	"github.com/gin-gonic/gin"
	"wskyygyso/api/app/http/controllers/api/v1"
	"wskyygyso/api/app/requests"
	"wskyygyso/api/pkg/captcha"
	"wskyygyso/api/pkg/logger"
	"wskyygyso/api/pkg/response"
	"wskyygyso/api/pkg/verifycode"
)

type VerifyCodeController struct {
	v1.BaseApiController
}

func (c *VerifyCodeController) ShowCaptcha(ctx *gin.Context) {
	id, b64s, err := captcha.NewCaptcha().GenerateCaptcha()
	logger.LogIf(err)
	response.JSON(ctx, gin.H{
		"captcha_id":    id,
		"captcha_image": b64s,
	})
}

func (c *VerifyCodeController) SendUsingPhone(ctx *gin.Context) {
	request := &requests.VerifyCodeRequest{}
	if ok := requests.Validate(ctx, request, requests.VerifyCodePhone); !ok {
		return
	}
	if ok := verifycode.NewVerifyCode().SendSms(request.Phone); !ok {
		response.Abort500(ctx, "发送短信失败")
	} else {
		response.SUCCESS(ctx)
	}
}

func (c VerifyCodeController) SendMail(ctx *gin.Context) {
	request := requests.VerifyCodeEmailRequest{}
	if ok := requests.Validate(ctx, &request, requests.VerifyCodeEmail); !ok {
		return
	}
	err := verifycode.NewVerifyCode().SendMail(request.Email)
	if err != nil {
		response.Abort500(ctx, "发送邮件失败")
	} else {
		response.SUCCESS(ctx)
	}

}
